<?php

namespace app\api\service;

use app\store\model\User;
use Hashids\Hashids;
use think\facade\Db;

class UserGradeService
{

    /**
     * 等级信息
     */
    public static  function findOne($id) {
        $rst = Db::name('user_grade')->where('id', $id)->where('status',1)->where('delete_time', 0)->find();
        return $rst;
    }

    /**
     * 计算会员等级优惠
     * return real_money 订单实际金额
     *        discount 会员等级折扣      
     */
    public static function gradeDiscount($id, $total)
    {
        $info = Db::name('user_grade')->where('id', $id)->where('status',1)->where('delete_time', 0)->find();
        if (empty($info)) {
            return 0;
        }
        if ($info['discount'] > 0) {
            $real_money = bcmul($total, $info['discount'], 2);
            $real_money > 0 ? $real_money : 0;
            $discount = bcsub($total, $real_money, 2);
            return [$real_money, $discount];
        } else {
            return [$total, 0];
        }
    }
}
